<template>
  <section class="config-all-box">
    <section class="news-main">
      <section>
        <el-collapse-transition>
          <section class="main-search merge">
            <span class="svg-container">
              <svg-icon icon-class="search" />
            </span>
            <el-input
              v-model.trim="listQuery.name"
              placeholder="输入配置名称搜索"
              class="search-input"
              @keyup.enter.native="handleFilter"
            />
            <section class="search-btn">
              <el-button class="filter-item" type="primary" @click="handleFilter">搜索</el-button>
              <el-button type="primary" @click="handleReset">重置</el-button>
            </section>
          </section>
        </el-collapse-transition>
        <section class="main-buttons">
          <section class="main-buttons-title">
            <span>积分配置</span>
          </section>
          <section class="main-buttons-btns">
            <el-button style="margin-left: 10px;" @click="handleCreate">新建</el-button>
          </section>
        </section>
        <section class="main-table">
          <v-table
            :list="list"
            tag="integralmanage/configAll"
            :list-loading="listLoading"
            :filter-column="filtercolumn"
            :selection="false"
          >
            <el-table-column label="操作" fixed="right" align="center" min-width="80" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <!--                <el-button
                  v-if="scope.row.configCode ==='INTEGRAL_SIGN_IN' ||scope.row.configCode ==='INTEGRAL_VIDEO' || scope.row.configCode ==='INTEGRAL_AUDIO'"
                  type="text"
                  size="mini"
                  @click="handleHonor(scope.row)"
                >
                  荣誉
                </el-button>-->
                <el-button type="text" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                <el-button type="text" size="mini" @click="handlePublish(scope.row)">{{ scope.row.status === 'ZT01' ? '禁用' : '启用' }}</el-button>
                <el-button type="text" size="mini" @click="handleDel(scope.row.id)">删除</el-button>
              </template>
            </el-table-column>
          </v-table>
          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="listQuery.page"
            :limit.sync="listQuery.limit"
            @pagination="getList"
          />
        </section>
      </section>
    </section>
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" center width="600px" top="25vh" :close-on-click-modal="false">
      <el-form ref="dataForm" :model="temp" :rules="rules" class="dataForm" label-position="right" label-width="110px" style="width: 550px;height:530px;overflow: auto;">
        <el-form-item label="配置名" prop="configName">
          <el-input v-model="temp.configName" placeholder="请输入配置名" />
        </el-form-item>
        <el-form-item label="配置值" prop="configValue">
          <el-input v-model="temp.configValue" disabled placeholder="请输入配置值" />
        </el-form-item>
        <el-form-item label="每日获得次数" prop="config1">
          <el-input v-model="temp.config1" placeholder="请输入每日获得次数" />
        </el-form-item>
        <el-form-item label="每次获取积分数量" prop="config2">
          <el-input v-model="temp.config2" placeholder="请输入每次获取积分数量" />
        </el-form-item>
        <el-form-item label="每日积分上限" prop="config3">
          <el-input v-model="temp.config3" placeholder="请输入每日积分上限" />
        </el-form-item>
        <el-form-item label="排序号" prop="configSort">
          <el-input v-model="temp.configSort" placeholder="请输入排序号" />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-radio-group v-model="temp.state">
            <el-radio :label="0">启用</el-radio>
            <el-radio :label="1">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="配置说明" prop="configRemark">
          <el-input v-model="temp.configRemark" :autosize="{ minRows: 2, maxRows: 4 }" type="textarea" placeholder="请输入配置说明" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button style="padding: 8px 30px;" @click="dialogFormVisible = false">取消</el-button>
        <el-button style="padding: 8px 30px;" type="primary" @click="updateData()">确定</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
import Pagination from '@/components/Pagination'
import VTable from '@/components/Table'
import {
  queryPageAll,
  updateStateAll,
  delAll,
  updateAll
} from '../api'
import permission from '@/directive/permission/index.js'
import elDragDialog from '@/directive/el-drag-dialog'
export default {
  name: 'IntegralConfigAll',
  components: { Pagination, VTable },
  directives: { permission, elDragDialog },
  data() {
    return {
      dialogStatus: '',
      dialogFormVisible: false,
      textMap: {
        create: '新增积分配置',
        update: '编辑积分配置'
      },
      temp: {
        configName: '',
        configSort: '',
        state: 0,
        configRemark: '',
        configValue: '',
        config1: '',
        config2: '',
        config3: ''
      },
      rules: {
        configName: [{ required: true, message: '请输入配置名称', trigger: 'blur' }],
        configSort: [{ required: true, message: '请输入排序', trigger: 'blur' }],
        state: [{ required: true, message: '请选择配置状态', trigger: 'change' }],
        configValue: [{ required: true, message: '请输入配置值', trigger: 'blur' }],
        config1: [{ required: true, message: '请输入每日获得次数', trigger: 'blur' }],
        config2: [{ required: true, message: '请输入每次获取积分数量', trigger: 'blur' }],
        config3: [{ required: true, message: '请输入每日积分上限', trigger: 'blur' }]
      },
      tag: 'integralmanage/configAll',
      listQuery: {
        page: 1,
        limit: 10,
        name: ''
      },
      list: [],
      total: 0,
      listLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          return new Promise((resolve, reject) => {
            const loading = this.$loading({
              lock: true,
              text: '请稍后',
              spinner: 'el-icon-loading',
              background: 'rgba(0, 0, 0, 0.1)'
            })
            updateAll(tempData).then(response => {
              this.dialogFormVisible = false
              loading.close()
              this.getList()
              this.$notify({
                title: '成功',
                message: '操作成功',
                type: 'success',
                duration: 2000
              })
            }).catch(error => {
              loading.close()
              reject(error)
            })
          })
        }
      })
    },
    /**
     * 编辑
     * */
    handleUpdate(row) {
      this.$router.push({
        path: this.$filterPath('manage'),
        query: {
          i: row.id || '1',
          type: 'u'
        }
      })
    },
    /**
     * 编辑
     * */
    handleHonor(row) {
      this.$router.push({
        path: this.$filterPath('honorManage'),
        query: {
          i: row.id || '1',
          t: row.configCode,
          n: row.configName
        }
      })
    },
    /**
     * 新建配置
     * */
    handleCreate() {
      this.$router.push({
        path: this.$filterPath('manage'),
        query: {
          type: 'c'
        }
      })
    },
    /**
     * 删除
     * */
    handleDel(ids) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({
          lock: true,
          text: '请稍后',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.3)'
        })

        return new Promise((resolve, reject) => {
          delAll({ id: ids }).then(() => {
            loading.close()
            this.$message({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          }).catch(error => {
            loading.close()
            reject(error)
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    /**
     * 启用/禁用
     */
    handlePublish(row) {
      this.$confirm(row.status === 'ZT01' ? '确认禁用？' : '确认启用？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        updateStateAll({ id: row.id, status: row.status === 'ZT01' ? 'ZT02' : 'ZT01' }).then(res => {
          if (res.data.code === '200') {
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
            this.getList()
          }
        })
      }).catch(() => {

      })
    },
    filtercolumn(scope, item) {
      return scope.row[item]
    },
    handleReset() {
      this.listQuery.page = 1
      this.listQuery.name = ''
      this.getList()
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },

    getList(obj) {
      this.listLoading = true
      var query = {
        page: obj ? obj.page : this.listQuery.page,
        pageSize: obj ? obj.limit : this.listQuery.limit,
        configName: this.listQuery.name
      }
      return new Promise((resolve, reject) => {
        queryPageAll(query).then(response => {
          this.list = response.data.data.rows
          this.total = response.data.data.total
          this.listLoading = false
        }).catch(error => {
          reject(error)
          this.listLoading = false
        })
      })
    }
  }
}
</script>
<style lang="scss">
.config-all-box{
  height: calc(100vh - 60px);
  padding: 0px;
  overflow: hidden;
  .downFile {
    display: inline-block;
    @include font-color($mainColor);
    font-size: 16px;
    float: right;
    text-decoration: underline;
  }
  .el-upload{
    display: block;
  }
  .el-upload-dragger{
    width: 100% !important;
  }

  .downFile:hover {
    cursor: pointer;
  }
  .vue-splitter-container{
    width: 100%;
  }
  .el-dropdown-link {
    cursor: pointer;
    font-size: 12px;
    @include font-color($mainColor);
  }

  .el-icon-arrow-down {
    font-size: 12px;
  }
  .treeBox {
    flex: 1;
    padding: 10px;
    margin-right: 0;
    background: #ffffff;
    height: 100%;
    background: #fff;
    overflow-y: auto;

    .search-tree-wrap {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .tree-filter {
        padding: 10px 18px;
      }
    }
  }

  .leftWrap {
    background: #fff;
    padding: 10px;
    .search-tree-wrap {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .tree-filter {
        padding: 10px 18px;
      }
    }

    .el-tree-node__content {
      margin-top: 10px;
    }

    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }

    .el-icon-caret-right:before {
      content: '\E7A0';
      font-size: 14px;
    }

    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before {
      content: '\E7A2';
      font-size: 14px;
    }
  }

  .news-main {
    padding: 10px;
    height: 100%;
    overflow-y: auto;
    .ts-con {
      position: absolute;
      top: 10px;
      left: 10px;
      right: 10px;
      bottom: 10px;
      background: white;
      .ts {
        line-height: 30px;
        position: absolute;
        left: 50%;
        top: 50%;
        margin-top: -15px;
        margin-left: -110px;
        width: 210px;
        text-align: center;
        background: white;
        border-radius: 2px;
        box-shadow: 1px 1px 5px #e0dbdb;
        font-size: 14px;
        font-family: Microsoft Yahei In-Bold, Microsoft Yahei,
        Apple LiGothic Medium;
        color: #222222;
      }
    }
    .main-search {
      background-color: #fff;
      padding: 0 10px;
      .svg-container {
        padding: 0 5px 0 10px;
        color: #bfced6;
        vertical-align: middle;
        display: inline-block;
      }
      .search-input {
        width: 45%;
        position: relative;
        &:after {
          content: '';
          position: absolute;
          left: -25px;
          right: 0;
          bottom: 8px;
          height: 1px;
          background: rgba(226, 227, 232, 1);
        }
        input {
          outline: none;
          background: transparent;
          border: none;
          outline: medium;
          padding: 0;
          &:focus {
            outline: none;
            background-color: transparent;
          }
        }
      }
      .search-btn {
        display: inline-block;
        float: right;
        .el-button.is-round {
          padding: 10px 30px;
          margin-right: 34px;
        }
        .search-more {
          font-size: 14px;
          font-weight: 500;
          @include font-color($mainColor);
          cursor: pointer;
          margin-left: 10px;
        }
      }

      &.merge {
        height: 50px;
        line-height: 50px;
      }

      &.separate {
        .search-inner {
          padding-top: 10px;
          .search-inner-item {
            .item-label {
              height: 30px;
              line-height: 30px;
              font-size: 14px;
              color: #a6a6a6;
              font-weight: 400;
            }
            .el-input {
              position: relative;
              &.el-date-editor input {
                padding-left: 30px;
              }
              .el-input__inner {
                border: none;
                padding: 0;
              }
              &:after {
                content: '';
                height: 1px;
                background-color: #e2e3e8;
                position: absolute;
                left: 0;
                right: 0;
                bottom: 0;
              }
            }
          }
        }
        .search-btn {
          text-align: center;
          display: block;
          float: none;
          height: 56px;
          line-height: 56px;
        }
      }
    }
    .classify-tab {
      width: 100%;
      margin: 10px 0;
      padding: 0 10px;
      background-color: #fff;
      display: flex;
      border-radius: 4px;
      .el-tabs__nav-wrap::after{
        display: none;
      }
      .el-tabs__header {
        margin: 0;
      }
    }
    .main-buttons {
      display: flex;
      line-height: 36px;
      margin-top: 10px;
      .main-buttons-title {
        flex: 1;
        height: 36px;
        line-height: 36px;
        font-size: 16px;
        font-weight: bold;
        color: rgba(51, 51, 51, 1);
      }
    }
  }
  .image-preview .el-dialog__body {
    text-align: center;
    .image-box {
      width: 200px;
      height: 80px;
      border: 2px dashed #ff9f91;
      border-radius: 5px;
    }
  }
}
</style>
